package com.example.demo.buessiness.user.controller;


import com.example.demo.buessiness.user.interfaces.IUserService;
import com.example.demo.buessiness.user.model.dto.UserRequest;
import com.example.demo.buessiness.user.model.dto.UserResponse;
import lombok.extern.slf4j.Slf4j;
import msa.common.model.ResultCode;
import msa.common.model.ResultModel;
import msa.common.utils.IDGenerator;
import msa.common.utils.ResultModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Size;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/fb")
@CrossOrigin(allowCredentials = "true", allowedHeaders = {"Authentication-Info", "content-type"},
        exposedHeaders = {"Authentication-Info"})

public class UserController {

    @Autowired
    IUserService userService;

    //查询
    @GetMapping("/user/findById/{id}")
    public ResultModel findById(@PathVariable("id") Long id){
        return userService.findById(id);
    }

    //添加
    @PostMapping(value = "/addUser",produces = "application/json;charset=UTF-8")
    public ResultModel addUser(@Validated @RequestBody UserRequest userRequest){
        return userService.addUser(userRequest);
    }

    //更新
    @PutMapping(value = "/updateUser",produces = "application/json;charset=UTF-8")
    public ResultModel updateUser(@RequestBody UserRequest userRequest){
        return userService.updateUser(userRequest);
    }

//    public static void main(String[] args) {
//        String id = IDGenerator.getId();
//        System.out.println(id);
//
//    }
    //删除
    @DeleteMapping("/delUser")
    public ResultModel deleteUser(@RequestParam("id") Long id){
        try {
            return userService.deleteUser (id);
        } catch (Exception e) {
            return ResultModelUtils.getFailInstance();
        }
    }

    //查询所有
    @GetMapping(value = "/userAll")
    public ResultModel findAll(){
        try {
            return ResultModelUtils.getSuccessInstance (userService.findAll());
        } catch (Exception e) {
            return ResultModelUtils.getFailInstance (ResultCode.BUSINESS_ERROR.toString (),e.getMessage ());
        }
    }

    //修改名称
    @PutMapping("/updateUsernameById")
    public ResultModel updateUsernameById(@RequestBody UserRequest userRequest){
            userService.updateUsernameById (userRequest.getUsername(),Long.parseLong(userRequest.getId()));
            return ResultModelUtils.getInstance();
    }

    //登录查询
    @PostMapping("/logByUsername")
    public ResultModel logByUsername(@RequestBody UserRequest userRequest){

        try {
            List<UserResponse> userResponses= userService.logByUsername (userRequest.getUsername(),
                    userRequest.getPassword());
            if (userResponses.isEmpty())
            {
                return ResultModelUtils.getInstance(false);
            }
            else
            {
                return ResultModelUtils.getSuccessInstance (userResponses);
            }
        } catch (Exception e) {
            return ResultModelUtils.getFailInstance (ResultCode.BUSINESS_ERROR.toString (),e.getMessage ());
        }
        //return ResultModelUtils.getSuccessInstance (userService.logByUsername (userRequest.getUsername(),userRequest.getPassword()));
    }

    //查询用户名
    @PostMapping("/findByUsername")
    public ResultModel findByUsername(@RequestBody UserRequest userRequest) {

        try {
            List<UserResponse> userResponses = userService.findByUsername(userRequest.getUsername());
            if (userResponses.isEmpty()) {
                return ResultModelUtils.getInstance(false);
            } else {
                return ResultModelUtils.getSuccessInstance(userResponses);
            }
        } catch (Exception e) {
            return ResultModelUtils.getFailInstance(ResultCode.BUSINESS_ERROR.toString(), e.getMessage());
        }
    }
}
